/**
 * Copyright 2009-2016 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.ibatis.session;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;

import com.hsh.test.Order;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

/**
 * SqlSession工厂类的构建类，即构建SqlSessionFactory实例，主要有三类方法：</br>
 * 1、{@link #build(Reader, String, Properties)} </br>
 * 	根据Reader创建SqlSessionFactory实例，通过XMLConfigBuilder类解析读取到的配置文件，并解析成Configuration实例对象，
 * 然后调用方法3，实现创建SqlSessionFactory实例。</br>
 * 2、{@link #build(InputStream, String, Properties)} </br>
 * 	根据InputStream创建SqlSessionFactory实例，同方法一类似。</br>
 * 3、{@link #build(Configuration)}</br>
 * 	根据Configuration创建SqlSessionFactory实例，该方法是真正实现创建工厂实例的方法，其他两类最后都是通过调用该方法实现创建工厂实例
 * 根据默认的DefaultSqlSessionFactory类创建SqlSessionFactory实例。
 *
 * Builds {@link SqlSession} instances.
 *
 * @author Clinton Begin
 */
public class SqlSessionFactoryBuilder {

	public SqlSessionFactory build(Reader reader) {
		return build(reader, null, null);
	}

	public SqlSessionFactory build(Reader reader, String environment) {
		return build(reader, environment, null);
	}

	public SqlSessionFactory build(Reader reader, Properties properties) {
		return build(reader, null, properties);
	}

	/**
	 * 通过Reader读取配置文件，然后通过XMLConfigBuilder解析读取到的配置文件，并生成Configuration实例，
	 * 然后调用{{@link #build(Configuration)}方法创建SqlSessionFactory对象。
	 * 底层真正构建SqlSessionFactory对象的方法是创建了一个DefaultSqlSessionFactory
	 * 	对象实例（通过带Configuration实例参数的构造函数实现）
	 * @param reader
	 * @param environment
	 * @param properties
	 * @return
	 */
	public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
		try {
			XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
			return build(parser.parse());
		} catch (Exception e) {
			throw ExceptionFactory.wrapException("Error building SqlSession.", e);
		} finally {
			ErrorContext.instance().reset();
			try {
				reader.close();
			} catch (IOException e) {
				// Intentionally ignore. Prefer previous error.
			}
		}
	}

	public SqlSessionFactory build(InputStream inputStream) {
		return build(inputStream, null, null);
	}

	public SqlSessionFactory build(InputStream inputStream, String environment) {
		return build(inputStream, environment, null);
	}

	public SqlSessionFactory build(InputStream inputStream, Properties properties) {
		return build(inputStream, null, properties);
	}

	/**
	 * 通过InputStream读取配置文件，然后通过XMLConfigBuilder解析读取到的配置文件，并生成Configuration实例，
	 * 然后调用{{@link #build(Configuration)}方法创建SqlSessionFactory对象，
	 * 底层真正构建SqlSessionFactory对象的方法是创建了一个DefaultSqlSessionFactory
	 * 	对象实例（通过带Configuration实例参数的构造函数实现）
	 * @param inputStream
	 * @param environment
	 * @param properties
	 * @return
	 */
	public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
		try {
			@Order(v="2.1" , d="将流读取解析成XMLConfigBuilder类实例,不同的environment生成不同的XMLConfigBuilder实例")
			XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
			return build(parser.parse());
		} catch (Exception e) {
			throw ExceptionFactory.wrapException("Error building SqlSession.", e);
		} finally {
			ErrorContext.instance().reset();
			try {
				inputStream.close();
			} catch (IOException e) {
				// Intentionally ignore. Prefer previous error.
			}
		}
	}

	@Order(v="2.3" , d="将Configuration实例转化为SqlSessionFactory实例")
	public SqlSessionFactory build(Configuration config) {
		return new DefaultSqlSessionFactory(config);
	}

}
